package com.winway.framework.sap.client;

import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.impl.httpclient3.HttpTransportPropertiesImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.Method;
import java.util.List;

public class SAPWebServiceClient implements ISAPWebServiceConstant{

    private static final Log logger = LogFactory.getLog(SAPWebServiceClient.class);

    /**
     * SAPMaster通用接口，根据传入参数可自动调用不同的接口
     * @param listArgs
     * @return
     * @throws Exception
     */
    public ZBF_MASTERStub.ZbfMaster[] SAPMasterWebServiceInit(List<String> listArgs) throws Exception{

        ZBF_MASTERStub service = new ZBF_MASTERStub();
        HttpTransportPropertiesImpl.Authenticator auth = new HttpTransportPropertiesImpl.Authenticator();
        auth.setUsername(USERNAME);
        auth.setPassword(PASSWORD);
        service._getServiceClient().getOptions()
            .setProperty(HTTPConstants.AUTHENTICATE, auth);

        ZBF_MASTERStub.ZbfMasterE param = new ZBF_MASTERStub.ZbfMasterE();
        Class<?> cls = param.getClass();
        int i = 0;
        for(String args:listArgs){
            ++i;
            ZBF_MASTERStub.Char20 c = new ZBF_MASTERStub.Char20();
            Method fieldSetMet = cls.getMethod(METHOD_PREFIX+i,c.getClass());
            c.setChar20(args);
            fieldSetMet.invoke(param, c);
        }

        //SAP设置output后才能获取output信息
        param.setOutput(new ZBF_MASTERStub.TableOfZbfMaster());

        ZBF_MASTERStub.ZbfMasterResponse zbfMasterResponse = service.zbfMaster(param);
        //获取SAP的output结果，后续业务处理使用
        ZBF_MASTERStub.TableOfZbfMaster output = zbfMasterResponse.getOutput();
        ZBF_MASTERStub.ZbfMaster[] item = output==null ? null:output.getItem();

        return item;

    }

    /**
     * SAPMaster008通用接口，根据传入参数可自动调用不同的接口
     *
     * @param listArgs
     * @return
     * @throws Exception
     */
    public ZBF_MASTERStub.ZbfMaster[] SAPMaster008WebServiceInit(List<String> listArgs) throws Exception {

        ZBF_MASTERStub service = new ZBF_MASTERStub();
        HttpTransportPropertiesImpl.Authenticator auth = new HttpTransportPropertiesImpl.Authenticator();
        auth.setUsername(USERNAME);
        auth.setPassword(PASSWORD);
        service._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE, auth);

        ZBF_MASTERStub.ZbfMasterE param = new ZBF_MASTERStub.ZbfMasterE();
        Class<?> cls = param.getClass();
        int i = 0;
        for (String args : listArgs) {
            ++i;
            ZBF_MASTERStub.Char20 c = new ZBF_MASTERStub.Char20();
            Method fieldSetMet = cls.getMethod(METHOD_PREFIX + i, c.getClass());
            c.setChar20(args);
            fieldSetMet.invoke(param, c);
        }

        // SAP设置output后才能获取output信息
        param.setOutput(new ZBF_MASTERStub.TableOfZbfMaster());

        ZBF_MASTERStub.ZbfMasterResponse zbfMasterResponse = service.zbfMaster(param);
        // 获取SAP的output结果，后续业务处理使用
        ZBF_MASTERStub.TableOfZbfMaster output = zbfMasterResponse.getOutput();
        ZBF_MASTERStub.ZbfMaster[] item = output == null ? null : output.getItem();

        return item;

    }
}
